package co.fitstart.admin.dao;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.persistence.annotation.Execute;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.finance.StudentExportRecord;
import co.fitstart.entity.site.Site;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.StudentTargetData;
import co.fitstart.entity.user.User;

/**
 *
 * @author Ark
 *
 */
@Dynamic("hibernate")
public interface StudentDao {
    
    /**
     * 
     * @param export
     * @param date
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Student student WHERE student.disabled = false AND exportArchives = :export AND createTime >= :date")
    public List<Student> list(@Variable("export") boolean export, @Variable("date") Date date, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
    /**
     * 
     * @param date
     * @return
     */
    @Query("FROM Student student WHERE student.disabled = false AND exportArchives = false")
    public List<Student> list();
    
    
    /**
     * 
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Student student WHERE student.disabled = false AND student.stat.onlineCount > 0")
    public List<Student> listOnlineStudent(@Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
    /**
     * 
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Student student WHERE student.disabled = false AND (student.stat.individualCount+student.stat.groupCount+student.stat.yearCount) > 0")
    public List<Student> listOfflineStudent(@Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
    /**
     * 
     * @param type
     * @param site
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("SELECT round.student FROM CourseRound round WHERE round.disabled = false AND round.courseType in (:type) AND round.site = :site AND round.student.disabled = false AND (round.student.stat.individualCount+round.student.stat.groupCount+round.student.stat.yearCount) > 0 GROUP BY round.student")
    public List<Student> listOfflineStudent(@Variable("type") CourseType[] type, @Variable("site") Site site, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * 
     * @param type
     * @param site
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("SELECT round.student FROM CourseRound round WHERE round.disabled = false AND round.courseType in (:type) AND round.site IN (:sites) AND round.student.disabled = false AND (round.student.stat.individualCount+round.student.stat.groupCount+round.student.stat.yearCount) > 0 GROUP BY round.student")
    public List<Student> listOfflineStudent(@Variable("type") CourseType[] type, @Variable("sites") List<Site> sites, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
    /**
     * 
     * @param user
     * @return
     */
    @Query("FROM Student WHERE disabled = false AND user = :user")
    public Student load(@Variable("user") User user);
    
    /**
     * 
     * @param mobile
     * @return
     */
    @Query("FROM Student WHERE disabled = false AND mobile = :mobile")
    public Student load(@Variable("mobile") String mobile);
    
    /**
     * 
     * @param mobile
     * @return
     */
    @Query("FROM Student student WHERE student.disabled = false AND student.stat.onlineCount > 0 AND student.mobile = :mobile")
    public Student loadOnlineStudent(@Variable("mobile") String mobile);
    
    /**
     * 
     * @param mobile
     * @return
     */
    @Query("FROM Student student WHERE student.disabled = false AND (student.stat.individualCount+student.stat.groupCount+student.stat.yearCount) > 0 AND student.mobile = :mobile")
    public Student loadOfflineStudent(@Variable("mobile") String mobile);
    
    /**
     * 
     * @param type
     * @param site
     * @param mobile
     * @return
     */
    @Query("SELECT round.student FROM CourseRound round WHERE round.disabled = false AND round.courseType in (:type) AND round.site = :site AND round.student.disabled = false AND (round.student.stat.individualCount+round.student.stat.groupCount+round.student.stat.yearCount) > 0 AND round.student.mobile = :mobile")
    public Student loadOfflineStudent(@Variable("type") CourseType[] type, @Variable("site") Site site, @Variable("mobile") String mobile);

    /**
     * 
     * @param type
     * @param sites
     * @param mobile
     * @return
     */
    @Query("SELECT round.student FROM CourseRound round WHERE round.disabled = false AND round.courseType in (:type) AND round.site IN (:sites) AND round.student.disabled = false AND (round.student.stat.individualCount+round.student.stat.groupCount+round.student.stat.yearCount) > 0 AND round.student.mobile = :mobile")
    public Student loadOfflineStudent(@Variable("type") CourseType[] type, @Variable("sites") List<Site> sites, @Variable("mobile") String mobile);
    
    /**
     * @param student
     * @param courseRound
     * @return
     */
    @Query("FROM StudentTargetData WHERE disabled = false AND student = :student AND courseRound = :courseRound ORDER BY createTime DESC")
    public List<StudentTargetData> listStudentTargetData(@Variable("student") Student student, @Variable("courseRound") CourseRound courseRound);
    
    /**
     * 
     * @param studentIds
     */
    @Execute("UPDATE Student SET exportArchives = true WHERE id in (:studentIds)")
    public void exportArchives(@Variable("studentIds") List<Long> studentIds);
    
    /**
     * 
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM StudentExportRecord ORDER BY createTime DESC")
    public List<StudentExportRecord> listExportRecord(@Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
    
}
